﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AnesSystem.Models;
using System.Collections.ObjectModel;
using AnesSystem.Services;
using AnesSystem;
using Common;

namespace AnesSystem.ViewModels
{
    public class AnesMethodsCtrlViewModel : MultitonBaseViewModel
    {
        #region Variables

        private List<ConfigAnesMethodsModel> _methodList;
        private List<ConfigAnesMethodItem> _methodItemList;
        private ObservableCollection<ConfigAnesMethodItem> _leftMethodList;
        private ObservableCollection<ConfigAnesMethodItem> _rightMethodList;
        private ObservableCollection<ConfigAnesMethodItem> _rightMethodAllList;
        private ObservableCollection<ConfigAnesMethodItem> _topMethodList;
        private bool _isChecked = false;
        private string _otherMethod;
        private string _methodOneString;
        private string _methodTwoString;
        private string _anesMethodFullName = "";
        private int _oneMethodIndex = -1;



        #endregion

        #region Constructor

        public AnesMethodsCtrlViewModel()
        {
            _methodList = new List<ConfigAnesMethodsModel>();
            _methodItemList = new List<ConfigAnesMethodItem>();
            _leftMethodList = new ObservableCollection<ConfigAnesMethodItem>();
            _rightMethodList = new ObservableCollection<ConfigAnesMethodItem>();
            _rightMethodAllList = new ObservableCollection<ConfigAnesMethodItem>();
            _topMethodList = new ObservableCollection<ConfigAnesMethodItem>();
            IntialMethods();
        }

        #endregion

        #region Properties
        public ObservableCollection<ConfigAnesMethodItem> LeftMethodList
        {
            get
            {
                return _leftMethodList;
            }

            set
            {
                _leftMethodList = value;
            }
        }

        public ObservableCollection<ConfigAnesMethodItem> RightMethodList
        {
            get
            {
                return _rightMethodList;
            }

            set
            {
                _rightMethodList = value;
                RaisePropertyChanged("RightMethodList");
            }
        }

        public ObservableCollection<ConfigAnesMethodItem> RightMethodAllList
        {
            get
            {
                return _rightMethodAllList;
            }

            set
            {
                _rightMethodAllList = value;
            }
        }



        public string MethodOneString
        {
            get
            {
                return _methodOneString;
            }

            set
            {
                _methodOneString = value;
            }
        }
        public bool IsChecked
        {
            get
            {
                return _isChecked;
            }

            set
            {
                _isChecked = value;
                RaisePropertyChanged("IsChecked");
            }
        }

        public string MethodTwoString
        {
            get
            {
                return _methodTwoString;
            }

            set
            {
                _methodTwoString = value;
                SetAnesMethod();
            }
        }

        public string AnesMethodFullName
        {
            get
            {
                return _anesMethodFullName;
            }

            set
            {
                _anesMethodFullName = value;
            }
        }

        public string OtherMethod
        {
            get
            {
                return _otherMethod;
            }

            set
            {
                _otherMethod = value;
                RaisePropertyChanged("OtherMethod");
                SetAnesMethod();
            }
        }

        public int OneMethodIndex
        {
            get
            {
                return _oneMethodIndex;
            }

            set
            {
                _oneMethodIndex = value;
            }
        }

        public ObservableCollection<ConfigAnesMethodItem> TopMethodList
        {
            get
            {
                return _topMethodList;
            }

            set
            {
                _topMethodList = value;
            }
        }


        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void IntialMethods()
        {
            InitTopMethod();
            string mzff = DataManager.Instance.CurrentPatientLink.AnesthesiaRecord.Mzff;
            if (!string.IsNullOrEmpty(mzff))
            {
                if (mzff.EndsWith(")"))
                {
                    if (mzff.Contains("(") && mzff.Contains(")"))
                    {
                        int a = mzff.IndexOf("(");
                        int b = mzff.IndexOf(")");
                        MethodOneString = mzff.Substring(0, a);
                        MethodTwoString = mzff.Substring(a + 1, b - a - 1);
                    }
                }
                else
                {
                    if (mzff.Contains("非插管全麻"))
                    {
                        AnesMethodFullName = "非插管全麻";
                        OtherMethod = mzff.Replace("非插管全麻", "").Trim();
                    }
                    if (mzff.Contains("无麻醉"))
                    {
                        AnesMethodFullName = "无麻醉";
                        OtherMethod = mzff.Replace("无麻醉", "").Trim();
                    }
                    if (mzff.Contains("静脉麻醉"))
                    {
                        AnesMethodFullName = "静脉麻醉";
                        OtherMethod = mzff.Replace("静脉麻醉", "").Trim();
                    }
                    else
                    {
                        if (mzff.Contains("(") && mzff.Contains(")"))
                        {
                            int a = mzff.IndexOf("(");
                            int b = mzff.IndexOf(")");
                            MethodOneString = mzff.Substring(0, a);
                            MethodTwoString = mzff.Substring(a + 1, b - a - 1);
                            OtherMethod = mzff.Substring(b + 1, mzff.Length - b - 1);
                        }
                    }
                }

            }

            foreach (var item in TopMethodList)
            {
                if (AnesMethodFullName.Contains(item.ConfigAnesMethod.Name) && !AnesMethodFullName.Contains("复合麻醉"))
                {
                    item.IsChecked = true;
                    item.SelectColor = "LightBlue";
                }
            }
            SetAnesMethod();
            _methodList = ConfigAnesMethodService.Instance.FindAll().ToList();
            foreach (var item in _methodList)
            {
                ConfigAnesMethodItem configAnesMethodItem = new ConfigAnesMethodItem();
                configAnesMethodItem.ConfigAnesMethod = item;
                _methodItemList.Add(configAnesMethodItem);
            }
            foreach (var item in _methodItemList)
            {
                if (item.ConfigAnesMethod.ParentId == 0)
                {
                    _leftMethodList.Add(item);
                    if (item.ConfigAnesMethod.Name.Equals(MethodOneString))
                    {
                        OneMethodIndex = item.ConfigAnesMethod.Index;
                    }
                }
                else
                {
                    _rightMethodAllList.Add(item);

                }

            }
            if (OneMethodIndex != -1)
            {
                foreach (var item in _rightMethodAllList)
                {
                    if (item.ConfigAnesMethod.ParentId == OneMethodIndex && item.ConfigAnesMethod.Name.Equals(MethodTwoString))
                    {
                        item.IsChecked = true;
                        item.SelectColor = "LightBlue";
                    }
                }
            }
        }

        private void InitTopMethod()
        {
            ConfigAnesMethodItem noAnesItem = new ConfigAnesMethodItem();
            ConfigAnesMethodsModel noAnes = new ConfigAnesMethodsModel();
            noAnes.Name = "无麻醉";
            noAnesItem.ConfigAnesMethod = noAnes;

            ConfigAnesMethodItem fcgqmItem = new ConfigAnesMethodItem();
            ConfigAnesMethodsModel fcgqm = new ConfigAnesMethodsModel();
            fcgqm.Name = "非插管全麻";
            fcgqmItem.ConfigAnesMethod = fcgqm;

            if (HospitalConfig.Instance.CurrentHospital.HospitalCode.Equals(HospitalConfig.HOSPITAL_CODE_CQ_FYBJY)) {
                ConfigAnesMethodItem jmmzItem = new ConfigAnesMethodItem();
                ConfigAnesMethodsModel jmmz = new ConfigAnesMethodsModel();
                jmmz.Name = "静脉麻醉";
                jmmzItem.ConfigAnesMethod = jmmz;
                TopMethodList.Add(jmmzItem);
            }
          
            TopMethodList.Add(noAnesItem);
            TopMethodList.Add(fcgqmItem);
            
        }

        #endregion

        #region Public/Protected Methods

        public void CheckChanged(int id)
        {

            foreach (var item in _rightMethodAllList)
            {

                if (item.ConfigAnesMethod.Index == id)
                {
                    item.IsChecked = true;
                    item.SelectColor = "LightBlue";
                }
                else
                {
                    item.IsChecked = false;
                    item.SelectColor = "Transparent";
                }
            }
        }
        public void unCheckChanged(int id)
        {

            foreach (var item in _rightMethodAllList)
            {

                item.IsChecked = false;
                item.SelectColor = "Transparent";

            }
        }
        public void CheckChangedTwo(int id)
        {
            _rightMethodList.Clear();

            foreach (var item in LeftMethodList)
            {
                if (item.ConfigAnesMethod.Index == id)
                {
                    item.IsChecked = true;
                    item.SelectColor = "LightBlue";
                }
                else
                {
                    item.IsChecked = false;
                    item.SelectColor = "Transparent";
                }
            }

            foreach (var item in _rightMethodAllList)
            {
                if (item.ConfigAnesMethod.ParentId == id)
                {
                    _rightMethodList.Add(item);
                }

            }
        }

        public override void InitData()
        {

        }

        public override void Refresh()
        {

        }

        public void SetAnesMethod()
        {
            if (!string.IsNullOrEmpty(MethodTwoString))
            {
                AnesMethodFullName = MethodOneString + "(" + MethodTwoString + ")" + OtherMethod;
            }
            if (AnesMethodFullName.Contains("非插管全麻") && !AnesMethodFullName.Contains("复合麻醉"))
            {
                if (string.IsNullOrEmpty(OtherMethod))
                {
                    AnesMethodFullName = "非插管全麻";
                }
                else
                {
                    AnesMethodFullName = AnesMethodFullName + OtherMethod;
                }

            }
            if (AnesMethodFullName.Contains("静脉麻醉"))
            {
                if (string.IsNullOrEmpty(OtherMethod))
                {
                    AnesMethodFullName = "静脉麻醉";
                }
                else
                {
                    AnesMethodFullName = AnesMethodFullName + OtherMethod;
                }
            }
        }

        #endregion

        #region EventHandlers

        #endregion
    }
}
